//
//  AY_QiuShuView.m
//  AiYue
//
//  Created by mac on 2023/12/25.
//

#import "AY_QiuShuView.h"
#import "AY_BookDetailViewController.h"
#import "AY_MainTabViewController.h"

@interface AY_QiuShuView()

@property (strong,nonatomic) UIView *backView;
@property (strong,nonatomic) UIImageView *bookImg;
@property (strong,nonatomic) UILabel *bookName;
@property (strong,nonatomic) UILabel *bookInfo;
@property (strong,nonatomic) UIButton *readBtn;
@property (strong,nonatomic) UIButton *cancelBtn;

@end

@implementation AY_QiuShuView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backView = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"#2C2435") andRadius:YNUM(12) andAddSubView:self];
        self.backView.layer.masksToBounds = NO;
        self.backView.layer.shadowColor = [UIColor lightGrayColor].CGColor; // 阴影颜色
        self.backView.layer.shadowOffset = CGSizeMake(0, 0); // 偏移距离
        self.backView.layer.shadowOpacity = 0.3; // 不透明度
        self.backView.layer.shadowRadius = 5.0; // 半径
        [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self).offset(YNUM(16));
            make.right.bottom.equalTo(self).offset(-YNUM(16));
        }];
        
        self.bookImg = [AYTool createImageViewWithFrame:CGRectZero andImage:nil andBg:nil andRadius:YNUM(8) andAddSubView:self.backView];
        [self.bookImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(YNUM(12));
            make.top.equalTo(self.backView).offset(YNUM(16));
            make.bottom.equalTo(self.backView).offset(-YNUM(16));
            make.width.mas_equalTo(YNUM(50));
        }];
        
        self.readBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"马上阅读" andFont:YFont(14) andTitleColor:COLOR(@"#702E18") andBg:COLOR(@"#F3BDA9") andImage:nil andBgImage:nil andTarget:self andAction:@selector(goToRead) andRadius:YNUM(16) andAddSubView:self.backView];
        [self.readBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.backView);
            make.right.equalTo(self.backView).offset(-YNUM(12));
            make.width.mas_equalTo(YNUM(100));
            make.height.mas_equalTo(YNUM(32));
        }];
        
        self.bookName = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#FFE1CA") andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:self.backView];
        [self.bookName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookImg.mas_right).offset(YNUM(12));
            make.top.equalTo(self.bookImg).offset(YNUM(5));
            make.right.equalTo(self.readBtn.mas_left).offset(-YNUM(10));
        }];
        
        self.bookInfo = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#BB9A82") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self.backView];
        [self.bookInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookImg.mas_right).offset(YNUM(12));
            make.top.equalTo(self.bookName.mas_bottom).offset(YNUM(15));
            make.right.equalTo(self.readBtn.mas_left).offset(-YNUM(10));
        }];
        
        self.cancelBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"X" andFont:YFont(14) andTitleColor:[UIColor whiteColor] andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(cancelAction) andRadius:0 andAddSubView:self.backView];
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.backView).offset(YNUM(5));
            make.right.equalTo(self.backView).offset(-YNUM(5));
            make.width.height.mas_equalTo(YNUM(16));
        }];
    }
    return self;
}

- (void)setModel:(QiuShuModel *)model {
    _model = model;
    [self.bookImg sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:YImage(@"placeholderImage")];
    self.bookName.text = @"您申请的书籍已上线";
    self.bookInfo.text = model.info;
}

- (void)cancelAction {
    [self removeFromSuperview];
    [AYApiRequest readQiuShuSuccess:^(AY_BaseModel * _Nonnull pkg) {
        
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (void)goToRead {
    [self removeFromSuperview];
    [AYApiRequest readQiuShuSuccess:^(AY_BaseModel * _Nonnull pkg) {
        
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    bookDetailVC.bookId = self.model.bookId;
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UINavigationController *nav = (UINavigationController *) window.rootViewController;
    AY_MainTabViewController *rootViewController = nav.viewControllers[0];
    [rootViewController.navigationController pushViewController:bookDetailVC animated:YES];
}
@end
